package com.uzai.console.web.turnlink;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.uzai.common.exception.BusinessException;
import com.uzai.console.dto.AtMiddlePageAreaDTO;
import com.uzai.console.dto.BaseDto;
import com.uzai.console.entity.AtMiddlePageArea;
import com.uzai.console.service.AtMiddlePageAreaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * 活动转链中间页区域代码片段(AtMiddlePageArea)表控制层
 *
 * @author szl
 * @since 2023 -09-19 15:22:13
 */
@RestController
@RequestMapping("/turnlink")
@Api(tags = "活动转链中间页区域代码片段控制器")
public class AtMiddlePageAreaController {
    /**
     * 服务对象
     */
    @Autowired
    private AtMiddlePageAreaService atMiddlePageAreaService;

    /**
     * 分页查询所有数据
     *
     * @param baseDto the base dto
     * @return 所有数据 object
     */
    @PostMapping("/middlePageTemplate/search")
    @ApiOperation("活动转链中间页区域代码片段分页查询")
    public Object search(@RequestBody BaseDto baseDto) {
        LambdaQueryWrapper<AtMiddlePageArea> condition = Wrappers.lambdaQuery(AtMiddlePageArea.class);
        condition.eq(AtMiddlePageArea::getMerId, baseDto.getMerId());
        return atMiddlePageAreaService.list(condition);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param source the source
     * @return 单条数据 object
     */
    @PostMapping("/middlePageTemplate/get")
    @ApiOperation("活动转链中间页区域代码片段详情")
    public Object get(@RequestBody AtMiddlePageAreaDTO source) {

        return this.atMiddlePageAreaService.getById(source.getId());
    }

    /**
     * 新增数据
     *
     * @param source 实体对象
     * @return 新增结果 object
     */
    @PostMapping("/middlePageTemplate/add")
    @ApiOperation("新增活动转链中间页区域代码片段")
    public Object save(@RequestBody AtMiddlePageAreaDTO source) {
        AtMiddlePageArea target = new AtMiddlePageArea();
        BeanUtil.copyProperties(source, target);
        target.setId(IdUtil.getSnowflakeNextId());
        target.setCreateTime((int) (System.currentTimeMillis() / 1000L));
        target.setUpdateTime((int) (System.currentTimeMillis() / 1000L));
        return this.atMiddlePageAreaService.save(target);
    }

    /**
     * 修改数据
     *
     * @param source 实体对象
     * @return 修改结果 object
     */
    @PostMapping("/middlePageTemplate/update")
    @ApiOperation("修改活动转链中间页区域代码片段")
    public Object update(@RequestBody AtMiddlePageAreaDTO source) {
        if (Objects.isNull(source) || Objects.isNull(source.getId())) {
            throw new BusinessException("参数缺失");
        }
        AtMiddlePageArea target = new AtMiddlePageArea();
        BeanUtil.copyProperties(source, target);
        target.setId(source.getId());
        target.setUpdateTime((int) (System.currentTimeMillis() / 1000L));
        return this.atMiddlePageAreaService.updateById(target);
    }

    /**
     * 删除数据
     *
     * @param source the source
     * @return 删除结果 object
     */
    @PostMapping("/middlePageTemplate/del")
    @ApiOperation("删除活动转链中间页区域代码片段")
    public Object delete(@RequestBody AtMiddlePageAreaDTO source) {
        if (Objects.isNull(source) || Objects.isNull(source.getId())) {
            throw new BusinessException("参数错误: 唯一值ID缺失");
        }
        return this.atMiddlePageAreaService.removeById(source.getId());
    }
}

